var answerlist = [];
function openpdf(name) {
    $("#pdfreader").show();
    $(".content").hide();
    $("#pdfreader").attr("src","https://cdn.qduwo.xyz/"+name+".pdf")
}
function rendermarkdown(id){
    $("#pdfreader").hide();
    $(".content").show();
    answerlist.forEach(value => {
        if(value.id == id){
            $(".content").html(marked(value.markdown));
        }
    })

}

window.onload = function () {
    $("img:first").replaceWith(
        "<div id=\"crackbox1\" style=\"border:none;width: 400px;max-height: 500px;overflow-y: scroll;overflow-x: scroll;position: absolute;top: 0; right: 0;cursor: move;z-index: 999999;\"><details><summary></summary><div class=\"inputsearch\" style=\"display: flex;flex-direction: row;\"><input type=\"text\" id=\"searchbox\"><button>Search</button></div><div class=\"menu\" style=\"display: flex;flex-direction: row;overflow-x: scroll;width: 100%;\"></div><div class=\"content\"></div><iframe id=\"pdfreader\" style=\"height: 300px;width: 400px;display: none\" src=\"./test.pdf\"></iframe></details></div>\n"
    )
    const div = document.getElementById("crackbox1");
    let isMouseDown = false; // 监听器，监听是否点击该 div 元素
    div.onmousedown = (e) => {
        isMouseDown = true; // 点击该 div 元素则设置监听器为 true
    }
    window.onmousemove = (e) => {
        if (!isMouseDown) {
            return;
        }
        div.style.left = e.clientX + 'px'; //动态获取并改变 left 的值
        div.style.top = e.clientY + 'px';
    }
    div.onmouseup = () => {
        isMouseDown = false;
    }
    let booklist = ["Hadoop","Android","Cloud"]
    booklist.forEach(value => {
        $(".menu").append(
            "<a onclick='openpdf(\""+value+"\")'>"+value+"</a>"
        )
    })

    $.ajax({
        url: "http://10.193.255.129:8000/getlist/",  // 发送的路径
        type: "get",  // 发送方式
        dataType: 'JSON',  // 反序列化
        success: (res) => {  // 成功获取到后端返回结果的回调函数
            answerlist = res.ans
            console.log(answerlist)
            answerlist.forEach(value => {
                $(".menu").append(
                    "<a onclick='rendermarkdown(\""+value.id+"\")'>"+value.name+"</a>"
                )
            })
        },
        error:()=>{  // 发送失败的回调函数
            console.log("失败");
        }
    })

    $(".content").html(marked("# Hadoop管理\n" +
        "\n" +
        "Hadoop管理包括集群规划设计、权限管理、NameNode HA、增加DataNode、减少DataNode "));

};
